﻿using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;

namespace RaycasterX
{
	public class Map
	{
		public List<Texture2D> Textures;
		public List<Sprite> Sprites = new List<Sprite>();
		public Texture2D Background;
		public int[,] WorldMap;

		internal Map(List<Texture2D> textures, Texture2D background, int[,] map)
		{
			Textures = textures;
			Background = background;

			WorldMap = map;
		}
	}

	public class MapReader : ContentTypeReader<Map>
	{
		protected override Map Read(ContentReader input, Map existingInstance)
		{
			//then we can read in all the textures
			int tCount = input.ReadInt32();
			List<Texture2D> textures = new List<Texture2D>();
			for (int i = 0; i < tCount; i++)
				textures.Add(input.ReadExternalReference<Texture2D>());

			//next the background texture
			Texture2D background = input.ReadExternalReference<Texture2D>();

			//and finall the map layout
			int rCount = input.ReadInt32();
			List<int[]> rows = new List<int[]>();
			for (int i = 0; i < rCount; i++)
				rows.Add(input.ReadObject<int[]>());

			//now we cram that map layout into the proper int[,] structure
			int[,] map = new int[rCount, rows[0].Length];
			for (int i = 0; i < rCount; i++)
				for (int j = 0; j < rows[i].Length; j++)
					map[i, j] = rows[i][j];

			//and return the new map object
			return new Map(textures, background, map);
		}
	}
}
